<?php

/**
 * This is the model class for table "{{user}}".
 *
 * The followings are the available columns in table '{{user}}':
 * @property integer $id
 * @property string $username
 * @property string $password
 * @property string $email
 * @property string $office
 * @property string $pname
 * @property string $position
 * @property string $phone
 * @property integer $power
 * @property integer $read
 * @property integer $dateline
 * @property integer $updated
 */
class User extends CActiveRecord {

    /**
     * Returns the static model of the specified AR class.
     * @return User the static model class
     */
    public static function model($className=__CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return '{{user}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('username', 'unique','message' => 'Username 已存在'),
            array('username, password, email, office, pname, position', 'required'),
            array('power, read, dateline, updated', 'numerical', 'integerOnly' => true),
            array('username, password, email, office', 'length', 'max' => 255),
            array('pname, position', 'length', 'max' => 100),
            array('phone', 'length', 'max' => 15),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, username, password, email, office, pname, position, phone, power, read, dateline, updated', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'id' => 'ID',
            'username' => 'Username',
            'password' => 'Password',
            'email' => 'Email',
            'office' => 'Office',
            'pname' => 'Pname',
            'position' => 'Position',
            'phone' => 'Phone',
            'power' => 'Power',
            'read' => '	Permission',
            'dateline' => 'Dateline',
            'updated' => 'Updated',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id);
        $criteria->compare('username', $this->username, true);
        $criteria->compare('password', $this->password, true);
        $criteria->compare('email', $this->email, true);
        $criteria->compare('office', $this->office, true);
        $criteria->compare('pname', $this->pname, true);
        $criteria->compare('position', $this->position, true);
        $criteria->compare('phone', $this->phone, true);
        $criteria->compare('power', $this->power);
        $criteria->compare('t.read', $this->read);
        $criteria->compare('dateline', $this->dateline);
        $criteria->compare('updated', $this->updated);

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }

    //根据用户的权限（power查找用户）
    public function getUserByPower($power) {
        return $this->findAll('power=:Power', array('Power' => (int) $power));
    }

    public function getUsers($power) {
        $users = User::model()->getUserByPower($power);
        if ($users) {
            $userArr = array();
            foreach ($users as $user) {
                $userArr[] = $user['username'];
            }
        }
        return $userArr;
    }

    public function validatePassword($password) {
        return $password === $this->password;
    }

    protected function beforeSave() {
        if (parent::beforeSave()) {
            if ($this->isNewRecord) {
                $this->dateline = $this->updated = time();
            } else {
                $this->updated = time();
            }
            return true;
        } else {
            return false;
        }
    }

}